import { clipboard } from 'electron'
import { showNotification } from '../utils//notification'
import { putCachedText } from './utils'
import Automator from './automator'
import Automation from './automation'
import * as window from './window'

const askMeAnythingId = '00000000-0000-0000-0000-000000000000'

export const notEditablePrompts = [askMeAnythingId]

export default class Commander {
  static init = async (): Promise<boolean> => {
    try {
      const _automator = new Automator()
      console.log('Nut Automator initiated.')
      return true
    } catch (error) {
      const message = error instanceof Error ? error.message : String(error)
      console.log(`Nut Automator cannot be initiated: ${message}`)
      return false
    }
  }
  static initCommand = async (timeout?: number): Promise<void> => {
    // not available in mas
    if (process.mas) {
      window.showMasLimitsDialog()
      return
    }

    // start time
    const startTime = Date.now()

    // get selected text
    const automator = new Automator()

    let text = await Automation.grabSelectedText(automator, timeout)

    if (text == null || text.trim() === '') {
      text = clipboard.readText()
    }

    // error
    if (text == null || text.trim() === '') {
      showNotification({
        body: 'No Text Selected'
      })
    }

    // go on with a cached text id
    const textId = putCachedText(text)
    const sourceApp = await automator.getForemostApp()
    window.openCommandPicker({ textId, sourceApp, startTime })
  }

  // execCommand = async (app: App, params: RunCommandParams): Promise<boolean> => {

  //   // deconstruct
  //   const { textId, sourceApp, command } = params;

  //   // get text
  //   const text = getCachedText(textId);

  //   try {

  //     // check
  //     if (!text) {
  //       console.error('No text to process');
  //       return false;
  //     }

  //     // config
  //     const config: Configuration = loadSettings(app);
  //     const llmManager: ILlmManager = LlmFactory.manager(config);

  //     // extract what we need
  //     let engine = command.engine || config.commands.engine;
  //     let model = command.model || config.commands.model;
  //     if (!engine?.length || !model?.length) {
  //       ({ engine, model } = llmManager.getChatEngineModel(false));
  //     }

  //     // template may be localized
  //     let template = command.template
  //     if (!template) {
  //       const t = useI18nLlm(app);
  //       template = t(`commands.commands.${command.id}.template`)
  //     }

  //     // build prompt
  //     const prompt = template.replace('{input}', text);

  //     // build the params
  //     const promptParams = {
  //       promptId: putCachedText(prompt),
  //       sourceApp: sourceApp,
  //       engine: engine || command.engine,
  //       model: model || command.model,
  //       execute: command.id != askMeAnythingId,
  //       action: params.action || 'default',
  //       replace: true,
  //     };

  //     // and open the window
  //     window.openPromptAnywhere(promptParams);
  //     return true;

  //   } catch (error) {
  //     console.error('Error while executing command', error);
  //   }

  //   // done
  //   return false;

  // }
}
